﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UbwApplication
{
    public partial class ucMircMessaging : UserControl
    {
        mIRCMessenger mirc;

        public Control uxLightsDetected;
        public ComboBox uxAnalogInputValues;
        public Control uxLightsTrigger;

        public ucMircMessaging()
        {
            InitializeComponent();

            mirc = new mIRCMessenger();
            UpdateMircWindowID();
            uxMircTriggerCommand.SelectedIndex = 0;
            getLastCommandTimer.Start();
        }

        internal void ReportException(Exception ex)
        {
            MessageBox.Show(ex.ToString(), ex.GetType().Name);
        }

        #region mIRC Controls
        private void ucSendmIRCMessage_Click(object sender, EventArgs e)
        {
            try
            {
                mirc.Command(uxCustomMircCommand.Text);
            }
            catch (Exception ex)
            {
                ReportException(ex);
            }
        }

        private void ucEvaluateMircExpression_Click(object sender, EventArgs e)
        {
            try
            {
                MessageBox.Show(mirc.Evaluate(uxCustomMircCommand.Text), "Response");
            }
            catch (Exception ex)
            {
                ReportException(ex);
            }
        }

        internal void UpdateMircWindowID()
        {
            mirc.UpdatemIRCWindowHandle();
            uxMircWindowID.Text = mirc.WindowHandle.ToString();
        }

        private void ucUpdateMircWindowID_Click(object sender, EventArgs e)
        {
            UpdateMircWindowID();
        }

        #endregion

        /// <summary>
        /// Get the last command to SpaceBox from mIRC
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void getLastCommandTimer_Tick(object sender, EventArgs e)
        {
            try
            {
                string command = mirc.Evaluate("%spaceboxcommand").Trim().ToLower();
                if (command != "")
                {
                    System.Diagnostics.Debug.Print("SPACEBOX COMMAND: " + command);
                }
                switch (command)
                {
                    case "lights":

                        string response = String.Format("Lights are {0} ({1} / {2})", uxLightsDetected.Text,
                            uxAnalogInputValues.Items[0].ToString(),
                            uxLightsTrigger.Text);

                        mirc.Command(uxMircTriggerCommand.Text + " " + response);
                        break;
                    case "shutup":
                        mirc.Command(uxMircTriggerCommand.Text + " k fine bye");
                        Application.Exit();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex) // TODO: dont do this
            {
                uxMessageLog.Items.Add((ex.GetType().Name + ": " + ex.Message));
            }
            finally
            {
                mirc.Command("/set %spaceboxcommand");
            }
        }


        protected bool lightsDetected = false;
        protected int lightsDetectedTriggerValue = 0;
        private void updateTriggers()
        {
            // Evaluate light sensor status
            int lightSensorValue = (int)uxAnalogInputValues.Items[0];
            int.TryParse(uxLightsTrigger.Text, out lightsDetectedTriggerValue);

            bool lightsDetectedNewValue = (lightSensorValue >= lightsDetectedTriggerValue);

            if (lightsDetectedNewValue)
            {
                uxLightsDetected.Text = "ON";
                uxLightsDetected.BackColor = Color.White;
            }
            else
            {
                uxLightsDetected.Text = "OFF";
                uxLightsDetected.BackColor = Color.DarkGray;
            }

            string mircCommandPrefix = uxMircTriggerCommand.Text;

            if ((lightsDetectedNewValue == true) && (lightsDetected == false))
                mirc.Command(mircCommandPrefix + " " + (char)3 + "7" + (char)2 + "The space is occupied");
            else if ((lightsDetectedNewValue == false) && (lightsDetected == true))
                mirc.Command(mircCommandPrefix + " " + (char)3 + "4" + (char)2 + "The space is no longer occupied");

            lightsDetected = lightsDetectedNewValue;
        }
    }
}
